<template>
	<zh-form layout="horizontal" class="form">
		<el-checkbox-group v-model="state.checkedList" @change="onchange">
			<zh-row :gutter="8">
				<zh-col :span="24"><el-checkbox :disabled="state.sceneDisabled" label="ALLSCENE">所有页面可见</el-checkbox></zh-col>
				<zh-col :span="24"><el-checkbox :disabled="state.functionDisabled" label="ALLFUNCTION">所有功能可操作</el-checkbox></zh-col>
				<zh-col :span="24"><el-checkbox :disabled="state.orgDisabled" label="ALLORG">所有组织权限</el-checkbox></zh-col>
				<zh-col :span="24"><el-checkbox :disabled="state.flowNodeDisabled" label="ALLFLOWNODE">所有流程节点权限</el-checkbox></zh-col>
			</zh-row>
		</el-checkbox-group>
	</zh-form>
</template>
<script lang="ts" setup name="SystemPermissionForm">
//------------------内部变量定义-------------------------
const props = defineProps({
	userInfo: {
		type: Object,
		default: () => {},
		required: true,
	},
});

//------------------内部变量定义-------------------------
const state = reactive({
	checkedList: [],
	sceneDisabled: true,
	functionDisabled: true,
	orgDisabled: true,
	flowNodeDisabled: true,
});

//------------------定义调用方法-------------------------
function initData() {
	if (props.userInfo) {
		const params = {
			userId: props.userInfo.userId,
		};
		setTimeout(() => {
			$api.common.send
				.post('user0014', params)
				.then((res) => {
					if (res.result.hasPermission && res.result.hasPermission.length > 0) {
						state.checkedList = res.result.hasPermission;
					}
					const userPermission = res.result.userPermission;
					if (userPermission && userPermission.length > 0) {
						for (var i = 0; i < userPermission.length; i++) {
							if (userPermission[i] === 'SUPERADMIN') {
								state.sceneDisabled = false;
								state.functionDisabled = false;
								state.orgDisabled = false;
								state.flowNodeDisabled = false;
								break;
							}
							if (userPermission[i] === 'ALLSCENE') {
								state.sceneDisabled = false;
							}
							if (userPermission[i] === 'ALLFUNCTION') {
								state.functionDisabled = false;
							}
							if (userPermission[i] === 'ALLORG') {
								state.orgDisabled = false;
							}
							if (userPermission[i] === 'ALLFLOWNODE') {
								state.flowNodeDisabled = false;
							}
						}
					}
				})
				.catch(() => {});
		}, 50);
	} else {
		state.checkedList = [];
	}
}
function onchange(checkedList) {
	const params = {
		userId: props.userInfo.userId,
		systemPrivileges: checkedList,
	};
	setTimeout(() => {
		$api.common.send
			.post('user0015', params)
			.then(() => {})
			.catch(() => {
				initData();
			});
	}, 50);
}

//------------------初始化------------------------------
initData();
</script>
